package com.example.rjb_shoping.service.impl;

import com.example.rjb_shoping.entity.RjbCommodity;
import com.example.rjb_shoping.entity.RjbCustomer;
import com.example.rjb_shoping.dao.RjbCustomerDao;
import com.example.rjb_shoping.query.CustomerQuery;
import com.example.rjb_shoping.service.RjbCustomerService;
import com.example.rjb_shoping.util.MailUtils;
import com.example.rjb_shoping.util.Md5Util;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (RjbCustomer)表服务实现类
 *
 * @author makejava
 * @since 2021-06-09 20:42:53
 */
@Service("rjbCustomerService")
public class RjbCustomerServiceImpl implements RjbCustomerService {
    @Resource
    private RjbCustomerDao rjbCustomerDao;


    @Override
    public RjbCustomer queryById(Integer cId) {
        return this.rjbCustomerDao.queryById(cId);
    }

    @Override
    public List<RjbCustomer> queryAllByLimit(int offset, int limit) {
        return this.rjbCustomerDao.queryAllByLimit(offset, limit);
    }

    @Value("${reset.url}")
    String url;

    @Override
    public List<RjbCustomer> likeAll(String nickName) {
        return rjbCustomerDao.likeAll(nickName);
    }

    @Override
    public RjbCustomer insert(RjbCustomer rjbCustomer) {
        rjbCustomer.setCImg("https://rjb-xqshoping.oss-cn-beijing.aliyuncs.com/rjb/img/flower/57713e70b9e6415588695c6ece21c3c2.jpg");
        this.rjbCustomerDao.insert(rjbCustomer);
        return rjbCustomer;
    }

    @Override
    public PageInfo<RjbCustomer> limitList(CustomerQuery customerQuery) {
        PageHelper.startPage(customerQuery.getPageNum(), customerQuery.getPageSize());
        RjbCustomer rjbCustomer = new RjbCustomer();
        rjbCustomer.setCStauts(customerQuery.getCStauts());
        final List<RjbCustomer> rjbAdmins = rjbCustomerDao.queryAll(rjbCustomer);
        PageInfo<RjbCustomer> pageInfo = new PageInfo<>(rjbAdmins);
        return pageInfo;
    }

    @Override
    public RjbCustomer update(RjbCustomer rjbCustomer) {
        try {
            rjbCustomer.setCPassword(Md5Util.encodeByMd5(rjbCustomer.getCPassword()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        int i = this.rjbCustomerDao.update(rjbCustomer);
        if (i > 0) {
            return this.queryById(rjbCustomer.getCId());
        }
        return null;
    }

    @Override
    public boolean deleteById(Integer cId) {
        return this.rjbCustomerDao.deleteById(cId) > 0;
    }

    @Override
    public RjbCustomer login(String username, String password) {
        RjbCustomer customer = rjbCustomerDao.login(username, password);
        return customer;
    }

    @Override
    public Integer registered(String nickName, String password, String phone, String address, String emil) {
        RjbCustomer rjbCustomer = this.selectOneName(nickName);
        if (rjbCustomer == null) {
            try {
                password = Md5Util.encodeByMd5(password);
            } catch (Exception e) {
                e.printStackTrace();
            }
            RjbCustomer r = new RjbCustomer();
            r.setCEmail(emil);
            r.setCNickname(nickName);
            r.setCPassword(password);
            r.setCPhone(phone);
            r.setCStauts(2); // 用户状态默认为1，表示正常
            r.setCAddress(address);
            r.setCImg("https://rjb-xqshoping.oss-cn-beijing.aliyuncs.com/rjb/img/main_picture/Avatar.png");
//            String txt = "<a href='http://" + url + "/backstage/log.html?name=" + nickName + "'>点击激活邮箱</a>";

            String txt = "<a href='http://" + url + "/backstage/backstage/log.html?name=" + nickName + "'>点击激活邮箱</a>";
            // http://10.0.200.108//backstage/backstage/index.html
            MailUtils.sendMail(r.getCEmail(), txt, "三宝果卉，用户激活");
            return rjbCustomerDao.insert(r);
        }
        return -1;
    }

    @Override
    public RjbCustomer selectOneName(String username) {
        return rjbCustomerDao.selectOneName(username);
    }

    @Override
    public boolean deleteByIdOne(Integer cId) {
        RjbCustomer rjbCustomer = this.queryById(cId);
        rjbCustomer.setCStauts(-1);
        RjbCustomer updateRjbCustomer = this.update(rjbCustomer);
        return updateRjbCustomer == null;
    }

    @Override
    public void setCustomerByName(String name) {
        int i = this.rjbCustomerDao.updateStatus(name);
    }

    @Override
    public int updateStatus(String username) {
        return this.rjbCustomerDao.updateStatus(username);
    }
}
